package com.boyi.cycling_master.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyi.cycling_master.R;
import com.boyi.cycling_master.bean.LoginResponseBean;
import com.boyi.cycling_master.bean.ResponseBean;
import com.boyi.cycling_master.common.SnackUtil;
import com.boyi.cycling_master.https.utils.UserRetrofit;
import com.boyi.cycling_master.ui.base.BaseActivity;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import talon.com.underlineText.UnderLineTextView;

/**
 * @author Lee_Mofeel
 * @version 1.0
 * @date 15:39
 * @Description 行天下账号注册
 */

public class RegisterXinActivity extends BaseActivity {
    @BindView(R.id.btn_nav_icon)
    ImageView btnNavIcon;
    @BindView(R.id.edt_register_user_name)
    MaterialEditText edtRegisterUserName;
    @BindView(R.id.edt_register_user_phone)
    MaterialEditText edtRegisterUserPhone;
    @BindView(R.id.edt_register_user_password)
    MaterialEditText edtRegisterUserPassword;
    @BindView(R.id.edt_register_confirm_password)
    MaterialEditText edtRegisterConfirmPassword;
    @BindView(R.id.tv_btn_register)
    TextView tvBtnRegister;
    @BindView(R.id.tv_underline_register)
    UnderLineTextView tvUnderlineRegister;

    @Override
    public int getContentViewId() {
        return R.layout.activity_register_xin;
    }


    @Override
    public void initLogic() {

    }

    @Override
    public void getBundleExtras(Bundle bundle) {

    }


    @OnClick({R.id.btn_nav_icon, R.id.tv_btn_register, R.id.tv_underline_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_nav_icon:
                finish();
                break;
            case R.id.tv_btn_register:
                String phoneNum = edtRegisterUserPhone.getText().toString();
                String loginName = edtRegisterUserName.getText().toString();
                String password = edtRegisterUserPassword.getText().toString();
                String confirm = edtRegisterConfirmPassword.getText().toString();
                if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(loginName) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirm)) {
                    SnackUtil.SnackShort(view,"请确认填写好相关数据");
                }else{
                    if(TextUtils.equals(password,confirm)){
                        registerXin(phoneNum,loginName,password);
                    }else{
                        SnackUtil.SnackShort(view,"两次密码不一致");
                    }
                }
                break;
            case R.id.tv_underline_register:
                break;
        }
    }

    public void registerXin(String phoneNum, String loginName, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("userPhone", phoneNum);
        map.put("userPassword", password);
        map.put("userName", loginName);
        map.put("userAddress", "成都");

        UserRetrofit.getInstance().toRegister(new Callback<LoginResponseBean>() {
            @Override
            public void onResponse(Call<LoginResponseBean> call, Response<LoginResponseBean> response) {
                LoginResponseBean bean = response.body();
                if(bean == null)
                    return;
                SnackUtil.SnackShort(tvBtnRegister,bean.getResultMessage());
                if(bean.getResultCode().equals("200") ){
                    //注册成功 保存数据 跳转到一下个界面
                    if(bean.getDataCollection().save())
                        startActivity(new Intent(RegisterXinActivity.this,RegisterFinishActivity.class));
                }
            }

            @Override
            public void onFailure(Call<LoginResponseBean> call, Throwable t) {
                SnackUtil.SnackShort(tvBtnRegister,t.getMessage());
                Log.d("PostDebug",t.getMessage());
            }
        },map);
    }

}
